#include "customheaderview.h"

#include <QDragEnterEvent>
#include <QDebug>
#include <QPainter>
#include <math.h>
#include <QDrag>
#include "dragdisplaywnd.h"
#include <QMimeData>
#include <QTableView>
#include <QStandardItemModel>
#include <QListWidgetItem>
#include <QListWidget>
#include "customdropeventsubwnd.h"

CustomHeaderView::CustomHeaderView(Qt::Orientation orientation, QWidget *parent):
    QHeaderView (orientation,parent),
    m_curDragCol(-1),
    mb_pressed(false)
{
    this->setAcceptDrops(true);
//    this->setSectionsClickable(false);
}

void CustomHeaderView::removeCurColumn()
{
    if(m_curDragCol==-1)return;
    QTableView* _tableView=static_cast<QTableView*>(this->parentWidget());
    QStandardItemModel* _model=static_cast<QStandardItemModel*>(_tableView->model()) ;
    _model->removeColumn(m_curDragCol);
}

void CustomHeaderView::addColumn(int pos, const QString &title)
{
    QTableView* _tableView=static_cast<QTableView*>(this->parentWidget());
    QStandardItemModel* _model=static_cast<QStandardItemModel*>(_tableView->model()) ;
    if(pos==-1)pos=_tableView->horizontalHeader()->count();
    _model->insertColumn(pos);
    QStandardItem* _item=new QStandardItem();
    _item->setTextAlignment(Qt::AlignCenter);
    _item->setText(title);
    _item->setTextAlignment(Qt::AlignCenter);
    _model->setHorizontalHeaderItem(pos,_item);
}

void CustomHeaderView::setHeaderItemLabel(int pos, const QString &content)
{
    QTableView* _tableView=static_cast<QTableView*>(this->parentWidget());
    QStandardItemModel* _model=static_cast<QStandardItemModel*>(_tableView->model()) ;
    _model->horizontalHeaderItem(pos)->setText(content);
}

void CustomHeaderView::dragEnterEvent(QDragEnterEvent *event)
{
    event->accept();
}


void CustomHeaderView::mousePressEvent(QMouseEvent *event)
{
    m_mousePressPoint=event->pos();
    mb_pressed=true;
    QHeaderView::mousePressEvent(event);
}

void CustomHeaderView::dropEvent(QDropEvent *event)
{
    QPoint _pos=event->pos();
    int _index=logicalIndexAt(_pos);
    qDebug()<<event->mimeData()->data("application/x-qstandarditemmodeldatalist");

    QString _format=QString("application/x-qabstractitemmodeldatalist");
    QByteArray encoded = event->mimeData()->data(_format);
    QDataStream stream(&encoded, QIODevice::ReadOnly);

    QVector<QMap<int, QVariant> > data;
    while (!stream.atEnd()) {
        int r, c;
        QMap<int, QVariant> v;
        stream >> r >> c >> v;
        data.append(v);
    }
    if(data.size()>0)
    {
        if(data[0].contains(0))
        {
            QString _str=(data[0][0]).toString();
            addColumn(_index,_str);
            CustomDropEventSubWnd* _subWnd=qobject_cast<CustomDropEventSubWnd*>(event->source());
            if(_subWnd)
             _subWnd->removeSelectItem();
        }
    }
}

void CustomHeaderView::mouseMoveEvent(QMouseEvent *event)
{
    QMutexLocker _lock(&m_dragMutex);
    if(mb_pressed
        &&(event->pos()-m_mousePressPoint).manhattanLength()>30
        &&cursor()!=Qt::SplitHCursor
        &&cursor()!=Qt::SplitVCursor)
    {
        QDrag* _drag=new QDrag(this);

        QMimeData* _md=new QMimeData;

        _drag->setMimeData(_md);

        DragDisplayWnd _dragDisplayWnd;

        QTableView* _tableView=static_cast<QTableView*>(this->parentWidget());

        if(_tableView==nullptr)
        {
            delete  _drag;
            return;
        }


        QStandardItemModel* _model=static_cast<QStandardItemModel*>(_tableView->model()) ;

        if(_model==nullptr)
        {
            delete  _drag;
            return;
        }

        int _index=logicalIndexAt(m_mousePressPoint.x());

        m_curDragCol=_index;
        if(m_curDragCol==-1)
        {
            delete  _drag;
            return;
        }

        QString _content=_model->horizontalHeaderItem(_index)->text();

        if(_content.isEmpty())
        {
            delete  _drag;
            return;
        }

        _md->setData("table/content",QByteArray(_content.toStdString().c_str()));
        _dragDisplayWnd.setDisplayContent(_content);
        QPixmap _pixmap=_dragDisplayWnd.grab();
        _drag->setPixmap(_pixmap);
        _drag->setHotSpot(QPoint(_pixmap.width()/2,_pixmap.height()/2));
        _drag->exec();
        delete _drag;
        m_curDragCol=-1;
        mb_pressed=false;
        qDebug()<<"m_curDragCol";
    }
    QHeaderView::mouseMoveEvent(event);
}

void CustomHeaderView::mouseReleaseEvent(QMouseEvent *event)
{
    mb_pressed=false;
    qDebug()<<"mb_pressed";
    QHeaderView::mouseReleaseEvent(event);
}

void CustomHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
}

void CustomHeaderView::startDrag(Qt::DropActions supportedActions)
{
    qDebug()<<"start drop";
}
